﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Security.AccessControl;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace Ele21RHelp.ViewMode
{
    /// <summary>
    /// 
    /// </summary>
    public class 预言 : ObservableObject
    {
        /// <summary>
        /// 
        /// </summary>
        private string _value1;
        /// <summary>
        /// 
        /// </summary>
        public string value1 { get => _value1; set { SetProperty(ref _value1, value); Sub(); } }
        /// <summary>
        /// 
        /// </summary>
        private string _value2;
        /// <summary>
        /// 
        /// </summary>
        public string value2 { get => _value2; set { SetProperty(ref _value2, value); Sub(); } }
        /// <summary>
        /// 
        /// </summary>
        private string _value3;
        /// <summary>
        /// 
        /// </summary>
        public string value3 { get => _value3; set { SetProperty(ref _value3, value); Sub(); } }
        /// <summary>
        /// 
        /// </summary>
        private string _value4;
        /// <summary>
        /// 
        /// </summary>
        public string value4 { get => _value4; set { SetProperty(ref _value4, value); Sub(); } }
        /// <summary>
        /// 
        /// </summary>
        private ObservableCollection<预言Mode> _item1;
        /// <summary>
        /// 上层
        /// </summary>
        public ObservableCollection<预言Mode> item1 { get => _item1; set => SetProperty(ref _item1, value); }
        /// <summary>
        /// 下层
        /// </summary>
        private ObservableCollection<预言Mode> _item2;
        /// <summary>
        /// 
        /// </summary>
        public ObservableCollection<预言Mode> item2 { get => _item2; set => SetProperty(ref _item2, value); }
        /// <summary>
        /// 
        /// </summary>
        private string _dstValue1;
        /// <summary>
        /// 
        /// </summary>
        public string dstValue1 { get => _dstValue1; set => SetProperty(ref _dstValue1, value); }

        /// <summary>
        /// 
        /// </summary>
        private string _dstValue2;
        /// <summary>
        /// 
        /// </summary>
        public string dstValue2 { get => _dstValue2; set => SetProperty(ref _dstValue2, value); }
        /// <summary>
        /// 
        /// </summary>
        private string _areType;
        /// <summary>
        /// 眼类型 0 闭眼 1睁眼
        /// </summary>
        public string areType { get => _areType; set => SetProperty(ref _areType, value); }

        /// <summary>
        /// 复制
        /// </summary>
        public ICommand CopyCommand { get; set; }

        public ICommand SelectareType { get; set; }
        public ICommand ClearCommand { get; set; }
        public 预言()
        {
            _value1 = "";
            _value2 = "";
            _value3 = "";
            _value4 = "";
            areType = "0";
            CopyCommand = new RelayCommand<string>(CopyMethod);
            SelectareType = new RelayCommand<string>(SelectareTypeMethod);
            ClearCommand = new RelayCommand(ClearCommandMethod);
            item2 = new ObservableCollection<预言Mode>();
            item1 = new ObservableCollection<预言Mode>();

            item1.Add(ModeCreate(0,1));
            item1.Add(ModeCreate(1,1));
            item1.Add(ModeCreate(2,2));
            item1.Add(ModeCreate(4,2));
            item1.Add(ModeCreate(6,1));
            item1.Add(ModeCreate(7,1));

            item2.Add(ModeCreate(0,2));
            item2.Add(ModeCreate(2,1));
            item2.Add(ModeCreate(3,1));
            item2.Add(ModeCreate(4,1));
            item2.Add(ModeCreate(5,1));
            item2.Add(ModeCreate(6,2));
        }
        private 预言Mode ModeCreate(int index,int sapn)
        {
            return new 预言Mode()
            {
                index = index,
                isOk = false,
                value = "0",
                span = sapn
            };
        }
        private void ClearCommandMethod()
        {
            value1 = "";
            value2 = "";
            value3 = "";
            value4 = "";
            dstValue1 = "";
            dstValue2 = "";
            foreach (var item in item1)
            {
                item.isOk = false;
                item.value = "0";
            }
            foreach (var item in item2)
            {
                item.isOk = false;
                item.value = "0";
            }
        }

        private void SelectareTypeMethod(string? obj)
        {
            areType = obj;
            Sub();
        }

        private void CopyMethod(string? obj)
        {
            string copyvalue = "";
            if (obj==dstValue1)
            {
                copyvalue += "上: ";
            }
            if (obj == dstValue2)
            {
                copyvalue += "下: ";
            }
            copyvalue += obj;
            HelpClass.CopyTo(copyvalue);
        }

        /// <summary>
        /// 计算
        /// </summary>
        private void Sub()
        {
            var v1 = ClearStr(value1);
            var v2 = ClearStr(value2);
            var v3 = ClearStr(value3);
            var v4 = ClearStr(value4);
            SubValueStr(v1.Trim(), item1);
            SubValueStr(v2.Trim(), item1);
            SubValueStr(v3.Trim(), item2);
            SubValueStr(v4.Trim(), item2);
            int index = 0;
            dstValue1 = "";
            foreach (var item in item1)
            {
                if (index%2==0)
                {
                    dstValue1 += " ";
                }
                dstValue1 += item.value;
                index++;
            }
            index = 0;
            dstValue2 = "";
            foreach (var item in item2)
            {
                if (index % 2 == 0)
                {
                    dstValue2 += " ";
                }
                dstValue2 += item.value;
                index++;
            }
            List<预言Mode> dstitem = new List<预言Mode>();
            dstitem.AddRange(item1);
            dstitem.AddRange(item2);
            foreach (var item in dstitem)
            {
                item.isOk = false;
                item.count = dstitem.FindAll(x => x.value == item.value).Count;
            }
            var max = dstitem.Max(x => x.count);
            var s= dstitem.Find(x => x.count == max);
            if (s==null)
            {
                return;
            }
            if (areType == "0")
            {
                //重复少
                foreach (var item in dstitem)
                {
                    if (item.value=="0")
                    {
                        continue;
                    }
                    if (item.count< max)
                    {
                        item.isOk= true;
                    }
                }
            }
            if (areType == "1")
            {
                //重复少
                foreach (var item in dstitem)
                { if (item.value=="0")
                    {
                        continue;
                    }
                    if (item.count == max)
                    {
                        item.isOk = true;
                    }
                }
            }
        }
        private void SubValueStr(string v, ObservableCollection<预言Mode> item1)
        {
            v = ClearStr(v);
            for (int i = 0; i < v.Length; i++)
            {
                if (v[i] != '0')
                {
                    SetValue(item1, i, v[i]);
                }
            }
        }
        private void SetValue(ObservableCollection<预言Mode> item1, int i, char v)
        {
            if (item1.Count > i)
            {
                item1[i].value = v.ToString();
            }
        }
        private string ClearStr(string value)
        {
            string dst = "";
            for (int i = 0; i < value.Length; i++)
            {
                if (value[i].ToString() != " ")
                {
                    dst += value[i].ToString();
                }
            }
            return dst;
        }
    }
    /// <summary>
    /// 
    /// </summary>
    public class 预言Mode : ObservableObject
    {
        /// <summary>
        /// 
        /// </summary>
        private int _index;
        /// <summary>
        /// 
        /// </summary>
        public int index { get => _index; set => SetProperty(ref _index, value); }
        /// <summary>
        /// 
        /// </summary>
        private int _span;
        /// <summary>
        /// 
        /// </summary>
        public int span { get => _span; set => SetProperty(ref _span, value); }
        /// <summary>
        /// 
        /// </summary>
        private string _value;
        /// <summary>
        /// 
        /// </summary>
        public string value
        {
            get => _value; set
            {
                SetProperty(ref _value, value);
                valueImage = string.Format("/image/num{0}.png", _value);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        private string _valueImage;
        /// <summary>
        /// 
        /// </summary>
        public string valueImage { get => _valueImage; set => SetProperty(ref _valueImage, value); }


        /// <summary>
        /// 
        /// </summary>
        private bool _isOk;
        /// <summary>
        /// 
        /// </summary>
        public bool isOk { get => _isOk; set { SetProperty(ref _isOk, value); isOkStr = _isOk ? "安全" : "不安全"; } }

        /// <summary>
        /// 
        /// </summary>
        private string _isOkStr;
        /// <summary>
        /// 
        /// </summary>
        public string isOkStr { get => _isOkStr; set => SetProperty(ref _isOkStr, value); }
        /// <summary>
        /// 
        /// </summary>
        private int _count;
        /// <summary>
        /// 
        /// </summary>
        public int count { get => _count; set => SetProperty(ref _count, value); }


    }
}
